// Tierless
// Copyright © 2010 Alphora
// This file is licensed under a modified BSD-license (see tierless_license.txt).
using System;
using System.Linq;

namespace Tierless.Framework.EntityTypes
{
	/// <summary>
	/// Represents a collection of <see cref="EntityKey"/> instances. 
	/// </summary>
	public class EntityKeyCollection : NamedEntityObjectTypeCollection<EntityKey>
	{
		protected override void InsertItem(int index, EntityKey item)
		{
			if (item.IsPrimary)
			{
				var pk = this.FirstOrDefault(k => k.IsPrimary);
				if (pk != null)
					throw new InvalidOperationException(String.Format("The key '{0}' is already marked as primary for this entity.", pk.Name));
			}

			base.InsertItem(index, item);
		}
	}
}